{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import os\n",
    "import os.path\n",
    "path = os.getenv(\"temp\")\n",
    "filename = os.path.join(path,\"info.txt\")\n",
    "root = tkinter.Tk()\n",
    "root[\"height\"] = 140\n",
    "root[\"width\"] = 200\n",
    "labelName = tkinter.Label(root,\n",
    "                             text='User Name:',\n",
    "                             justify=tkinter.RIGHT,\n",
    "                             anchor = \"e\",\n",
    "                             width=80)\n",
    "labelName.place(x=10,y=5, width=80, height=20)\n",
    "varName = tkinter.StringVar(root, value='')\n",
    "entryName = tkinter.Entry(root,\n",
    "                             width=80,\n",
    "                             textvariable=varName)\n",
    "entryName.place(x=100,y=5, width=80, height=20)\n",
    "labelPwd = tkinter.Label(root,\n",
    "                             text='User Pwd:',\n",
    "                             justify=tkinter.RIGHT,\n",
    "                             anchor = 'e',\n",
    "                             width=80)\n",
    "labelPwd.place(x=10,y=30, width=80, height=20)\n",
    "varPwd = tkinter.StringVar(root, value='')\n",
    "entryPwd = tkinter.Entry(root,\n",
    "                             show='*',\n",
    "                             width=80,\n",
    "                             textvariable=varPwd)\n",
    "entryPwd.place(x=100, y=30, width=80, height=20)\n",
    "try:\n",
    "    with open(filename) as fp:\n",
    "        n, p = fp.read().strip().split(',')\n",
    "        varName.set(n)\n",
    "        varPwd.set(p)\n",
    "except:\n",
    "    pass\n",
    "rememberMe = tkinter.IntVar(root, value=1)\n",
    "checkRemember = tkinter.Checkbutton(root,\n",
    "                                     text='Remember me?',\n",
    "                                     variable=rememberMe,\n",
    "                                     onvalue=1,\n",
    "                                     offvalue=0)\n",
    "checkRemember.place(x=30, y=70, width=120, height=20)\n",
    "def login():\n",
    "    name = entryName.get()\n",
    "    pwd = entryPwd.get()\n",
    "    if name=='admin' and pwd=='123456':\n",
    "        tkinter.messagebox.showinfo(title='恭喜',\n",
    "                                       message='登录成功！')\n",
    "        if rememberMe.get() == 1:\n",
    "            with open(filename,'w')as fp:\n",
    "                fp.write(','.join((name,pwd)))\n",
    "        else:\n",
    "            try:\n",
    "                os.remove(filename)\n",
    "            except:\n",
    "                pass\n",
    "    else:\n",
    "        tkinter.messagebox.showerror('警告',\n",
    "                                        message='用户名或密码错误' )\n",
    "buttonOk = tkinter.Button(root,\n",
    "                             text='Login',\n",
    "                             command=login)\n",
    "buttonOk.place(x=30, y=100, width=50, height=20)\n",
    "def cancel():\n",
    "    varName.set('')\n",
    "    varPwd.set('')\n",
    "buttonCancel = tkinter.Button(root,\n",
    "                                   text='Cancel',\n",
    "                                   command=cancel)\n",
    "buttonCancel.place(x=90, y=100, width=50, height=20)\n",
    "root.mainloop()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
